<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            height: 3000px;
        }

        .header {
            width: 100%;
            line-height: 80px;
            text-align: center;
            font-size: 30px;
            color: #fff;
            background-color: lightblue;
            position: fixed;
            opacity: 0;
            transition: opacity .5s linear;
        }

        .gotop {
            padding: 5px;
            width: 50px;
            height: 50px;
            background-color: pink;
            font-size: 20px;
            text-align: center;
            color: #fff;
            position: fixed;
            bottom: 50px;
            right: 60px;
            opacity: 0;
            cursor: pointer;
        }  
    </style>
</head>
<body>
    <div class="header">顶部导航</div>
    <div class="gotop">回到顶部</div>
    <script>
         var header = document.querySelector(".header");
        var gotop = document.querySelector(".gotop");
        window.onscroll = function(){
            var scrollTop = document.documentElement.scrollTop;
            
            if(scrollTop>500){
                header.style.opacity = 1;
                gotop.style.opacity = 1;
            }
            else{
                header.style.opacity = 0;
                gotop.style.opacity = 0;
            }
        }
        gotop.onclick = function(){
            window.scrollTo({
                top:0,
                behavior:"smooth"
            }
            )
        }
    </script>
</body>
</html>